﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Collections.Generic;

namespace WPControla.Client.Navigation
{
    public class navController
    {
        //Singleton, solo puede existir una instancia de esta clase.
        static navController current;
        public static navController Current
        {
            get
            {
                if (current == null)
                {
                    current = new navController();
                }
                return current;
            }
        }

        /// <summary>
        /// Método a ejecutar con parámetros de navegación.
        /// </summary>
        Action<NavigationEventArgs> NavigationMethod;

        /// <summary>
        /// Constructor
        /// </summary>
        public navController()
        {
            registeredViews.Add("Start", new Uri(@"/MainPage.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Guarda una relación Identificador <-> Uri
        /// </summary>
        static Dictionary<String, Uri> registeredViews = new Dictionary<String, Uri>();

        /// <summary>
        /// Navegar a un target, sin parámetros.
        /// </summary>
        /// <param name="navigationTarget"></param>
        public void NavigateTo(String navigationTarget)
        {
            PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
            rootFrame.Navigate(registeredViews[navigationTarget]);
        }

        /// <summary>
        /// Navegar a un target, con parámetros Uri
        /// </summary>
        /// <param name="navigationTarget"></param>
        /// <param name="UrlParams"></param>
        public void NavigateTo(String navigationTarget, String UrlParams)
        {
            PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
            String newUrl = String.Format(@"{0}?{1}", registeredViews[navigationTarget].ToString(), UrlParams);
            rootFrame.Navigate(new Uri(newUrl, UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// Navegar a un target, con parámetros complejos.
        /// </summary>
        /// <param name="navigationTarget"></param>
        /// <param name="onNavigated"></param>
        public void NavigateTo(String navigationTarget, Action<NavigationEventArgs> onNavigated)
        {
            PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
            rootFrame.Navigate(registeredViews[navigationTarget]);
            NavigationMethod = onNavigated;
            rootFrame.Navigated += new System.Windows.Navigation.NavigatedEventHandler(root_Navigated);
        }

        /// <summary>
        /// Este evento se lanza cuando hemos navegado a una página.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void root_Navigated(object sender, NavigationEventArgs e)
        {
            if (NavigationMethod != null)
            {
                NavigationMethod(e);
                NavigationMethod = null;
            }
        }
    }
}
